<%
# Copyright (C) 2011 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
%>

<% multi_answer ||= nil %>
<% answer = multi_answer %>
<% question ||= {} %>
<% answer_text = (hash_get(answer, :html).present? ? hash_get(answer, :html).html_safe : hash_get(answer, :text)) || '' %>
<% answer_label = html_to_text(answer_text + ' ' + (hash_get(question, 'question_text') || '')) %>
<div class="answer">
  <% if question_type.entry_type == "radio" || question_type.entry_type == "select" || question_type.entry_type == "checkbox" %>
    <table style="width: 100%">
      <tr>
        <td class="answer_input">
          <% if user_answer %>
            <%= "*" if hash_get(user_answer, :answer_id) == hash_get(answer, :id) %>
            <%= "+" if hash_get(answer, :weight) == 100 %>
          <% elsif question_type.entry_type == "checkbox" %>
            <input type="hidden" name="question_<%= hash_get(question, :id) %>_answer_<%= hash_get(answer, :id) %>" value="0"/>
            <input type="checkbox" <%= "checked" if @stored_params["question_#{hash_get(question, :id)}_answer_#{hash_get(answer, :id)}"].to_s == "1" %> class="question_input" name="question_<%= hash_get(question, :id) %>_answer_<%= hash_get(answer, :id) %>" value="1" id="question_<%= hash_get(question, :id) %>_answer_<%= hash_get(answer, :id) %>" />
          <% else %>
            <input type="radio" <%= "checked" if @stored_params["question_#{hash_get(question, :id)}"].to_s == hash_get(answer, :id).to_s %> class="question_input" name="question_<%= hash_get(question, :id) %>" value="<%= hash_get(answer, :id) %>" id="question_<%= hash_get(question, :id) %>_answer_<%= hash_get(answer, :id) %>" />
          <% end %>
        </td>
        <td class="answer_label">
          <label style="width: 100%" for="question_<%= hash_get(question, :id) %>_answer_<%= hash_get(answer, :id) %>"><%= answer_text %></label>
        </td>
      </tr>
    </table>
  <% elsif question_type.entry_type == "matching" %>
    <div style="float: left; width: 40%; margin-top: 10px;">
      <% if answer %>
        <label for="question_<%= hash_get(question, :id) %>_answer_<%= hash_get(answer, :id) %>" aria-label="<%= answer_label %>"><%= (hash_get(answer, :left_html) || hash_get(answer, :html)).present? ? (hash_get(answer, :left_html) || hash_get(answer, :html)).html_safe : (hash_get(answer, :left) || hash_get(answer, :text)) %></label>
        <% if !user_answer %>
          <br/>
          <div style="margin-left: 20px;">
          </div>
        <% end %>
      <% end %>
    </div>
    <div style="float: left; width: 40%; margin-top: 10px;">
      <% if user_answer %>
        <% if hash_get(user_answer, "answer_#{hash_get(answer, :id)}").to_i == hash_get(answer, :match_id).to_i %>
          <%= (hash_get(answer, :right) || (hash_get(question, :matches, []).find{|a| hash_get(a, :match_id).to_i == hash_get(answer, :match_id).to_i}[:text] rescue '')) %>
        <% else %>
          <%= hash_get(answer, :match_id) %>
          <%= hash_get(user_answer, "answer_#{hash_get(answer, :id)}") %>
          You put: <%= (hash_get(question, :answers, []).find{|a| hash_get(a, :match_id) == hash_get(user_answer, "answer_#{hash_get(answer, :id)}")}) %><br/>
          It was really: <%= (hash_get(answer, :right) || (hash_get(question, :matches, []).find{|a| hash_get(a, :match_id).to_i == hash_get(answer, :match_id).to_i}[:text] rescue '')) %>
        <% end %>
      <% else %>
        <select name="question_<%= hash_get(question, :id) %>_answer_<%= hash_get(answer, :id) %>" id="question_<%= hash_get(question, :id) %>_answer_<%= hash_get(answer, :id) %>" class="question_input" style="max-width: 90%;" aria-label="<%= answer_label %>">
          <option value="" <%= "selected" unless @stored_params["question_#{hash_get(question, :id)}_answer_#{hash_get(answer, :id)}"] %>>[ <%= t :choose_answer, "Choose" %> ]</option>
          <% right_answers.uniq{|a| hash_get(a, :right) || (hash_get(question, :matches, []).find{|aa| hash_get(aa, :match_id).to_i == hash_get(a, :match_id).to_i}[:text] rescue '') }.each do |a| %>
            <option value="<%= hash_get(a, :match_id) %>" <%= "selected" if @stored_params["question_#{hash_get(question, :id)}_answer_#{hash_get(answer, :id)}"].to_s == hash_get(a, :match_id).to_s %>><%= (hash_get(a, :right) || (hash_get(question, :matches).find{|aa| hash_get(aa, :match_id).to_i == hash_get(a, :match_id).to_i}[:text] rescue '')) %></option>
          <% end %>
        </select>
      <% end %>
    </div>
    <div class="clear"></div>
  <% end %>
</div>
<% if user_answer && (hash_get(user_answer, :answer_id) == hash_get(answer, :id) || (question_type.type == "matching" && hash_get(user_answer, "answer_#{hash_get(answer, :id)}").to_i != hash_get(answer, :match_id).to_i)) %>
<div>
  <%= hash_get(answer, :comments) %>
</div>
<% end %>
